package com.wangjiacai.utils.utils;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.*;
import java.util.function.BiConsumer;
import java.util.function.BiFunction;
import java.util.function.Function;

/**
 * @author jiacai.wjc
 * @description
 * @date 2020/12/19 6:20 下午
 */
public class ImageUtil {

    /**
     * 改变图片的大小到宽为size，然后高随着宽等比例变化
     *
     * @param in     上传的图片的输入流
     * @param out    改变了图片的大小后，把图片的流输出到目标OutputStream
     * @param format 新图片的格式
     * @throws IOException
     */
    public static OutputStream resizeImage(InputStream in, OutputStream out, String format, Function<int[], int[]> function) throws IOException {
        BufferedImage prevImage = ImageIO.read(in);
        int[] ret = function.apply(new int[]{prevImage.getWidth(), prevImage.getHeight()});
        BufferedImage image = new BufferedImage(ret[0], ret[1], BufferedImage.TYPE_INT_BGR);
        Graphics graphics = image.createGraphics();
        graphics.drawImage(prevImage, 0, 0, ret[0], ret[1], null);
        ImageIO.write(image, format, out);
        out.flush();
        in.close();
        out.close();
        return out;
    }

    /**
     * 改变图片的大小到宽为size，然后高随着宽等比例变化
     *
     * @param in     上传的图片的输入流
     * @param out    改变了图片的大小后，把图片的流输出到目标OutputStream
     * @param format 新图片的格式
     * @throws IOException
     */
    public static OutputStream resizeImage(InputStream in, OutputStream out, String format, float xScale, float yScale) throws IOException {
        BufferedImage prevImage = ImageIO.read(in);
        int wedth = (int) (prevImage.getWidth() * xScale);
        int height = (int) (prevImage.getHeight() * yScale);
        BufferedImage image = new BufferedImage(wedth, height, BufferedImage.TYPE_INT_BGR);
        Graphics graphics = image.createGraphics();
        graphics.drawImage(prevImage, 0, 0, wedth, height, null);
        ImageIO.write(image, format, out);
        out.flush();
        in.close();
        out.close();
        return out;
    }

    /**
     * 改变图片的大小到宽为size，然后高随着宽等比例变化
     *
     * @param in     上传的图片的输入流
     * @param out    改变了图片的大小后，把图片的流输出到目标OutputStream
     * @param format 新图片的格式
     * @throws IOException
     */
    public static OutputStream resizeImage(InputStream in, OutputStream out, String format, float scale) throws IOException {
        resizeImage(in,out,format,scale,scale);
        return out;
    }

    /**
     * 改变图片的大小到宽为size，然后高随着宽等比例变化
     *
     * @param in     上传的图片的输入流
     * @param out    改变了图片的大小后，把图片的流输出到目标OutputStream
     * @param width  新图片的宽
     * @param height 新图片的宽
     * @param format 新图片的格式
     * @throws IOException
     */
    public static OutputStream resizeImage(InputStream in, OutputStream out, int width, int height, String format) throws IOException {
        BufferedImage prevImage = ImageIO.read(in);
        BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_BGR);
        Graphics graphics = image.createGraphics();
        graphics.drawImage(prevImage, 0, 0, width, height, null);
        ImageIO.write(image, format, out);
        out.flush();
        in.close();
        out.close();
        return out;
    }

    /**
     * 改变图片的大小到宽为size，然后高随着宽等比例变化
     *
     * @param in     上传的图片的输入流
     * @param out    改变了图片的大小后，把图片的流输出到目标OutputStream
     * @param width  新图片的宽
     * @param height 新图片的宽
     * @throws IOException
     */
    public static void resizeImage(File in, File out, int width, int height) throws IOException {
        String format = FileUtil.getFileExt(out.getName()).toLowerCase();
        if (format == null) {
            return;
        }
        if (!format.equals("jpg") && !format.equals("png") && !format.equals("jpeg")) {
            return;
        }
        resizeImage(new FileInputStream(in), new FileOutputStream(out), width, height, format);
    }

    /**
     * 读取某个目录下所有文件、文件夹
     *
     * @param path
     * @return LinkedHashMap<String,String>
     */
    public static void resizeImageDir(String path, String targetPath, int width, int height) throws IOException {
        File file = new File(path);
        File[] tempList = file.listFiles();

        for (int i = 0; i < tempList.length; i++) {
            File file1 = tempList[i];
            if (file1.isDirectory()) {
                File dir = new File(targetPath, file1.getName());
                if (!dir.exists()) {
                    dir.mkdirs();
                }
                resizeImageDir(path + "/" + file1.getName(), targetPath + "/" + file1.getName(), width, height);
            } else {
                File target = new File(targetPath, file1.getName());
                resizeImage(file1, target, width, height);
            }
        }
    }
}
